﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1 {
    class Map {
        public int xPosition;
        public int yPosition;
        private int width;
        private int height;
        private bool onBuilding = false;
        private bool[] routes;
        private String[][] myMap;
        private ConsoleHandler ch;
        private Story s;

        /// <summary>
        /// Map for The Game
        /// </summary>
        /// <param name="xStart">Start x position of the player in the map</param>
        /// <param name="yStart">Start y position of the player in the map</param>
        public Map(ConsoleHandler ch) {
            this.ch = ch;

            //Index
            //s = Start
            //r = Road
            //x = Wall
            //b = Building
            //e = end point
            //t = tree
            myMap = new String[][] {
                new String [] {"s","r","r","r","r","r"},
                new String [] {"r","x","x","r","x","r"},
                new String [] {"r","b","x","r","b","r"},
                new String [] {"r","r","r","r","x","t"},
                new String [] {"r","x","b","r","x","r"},
                new String [] {"x","x","x","x","x","e"}
            };
            //find s
            int x = 0, y = 0;
            foreach (string[] row in myMap) {
                x = 0;
                foreach (string item in row) {
                    if (myMap[x][y].Equals("s")) {
                        this.xPosition = x;
                        this.yPosition = y;
                    }
                    x++;
                }
                y++;
            }

            this.width = x;
            this.height = y;
            //Walk();
        }

        internal void setStory(Story story) {
            this.s = story;
        }

        public void Walk() {
            for (; ; ) {
                printMap();

                ch.writeLine();

                CheckWalk();

                ch.clearScreen();
            }
        }
        public void CheckWalk() {
            route();
            int input = 0;
            for (; ; ) {
                if (onBuilding) {
                    input = ch.readInt(1, 5);
                } else {
                    input = ch.readInt(1, 4);
                }
                //ch.writeLine("" + input);
                if (input == 5) {
                    ch.writeLine("in building?");
                    s.tellStory("presskey_clearscreen");
                } else {
                    if (routes[input - 1] == true) {
                        switch (input) {
                            case 1://north
                                yPosition--;
                                break;
                            case 2://east
                                xPosition++;
                                break;
                            case 3://south
                                yPosition++;
                                break;
                            case 4://west
                                xPosition--;
                                break;
                            case 5:
                                break;
                        }
                        break;
                    } else {
                        ch.resetLine();
                        ch.writeLine("\nCan't go that way");
                        Console.SetCursorPosition(0, Console.CursorTop - 2);
                    }
                }
            }
            //ch.write("checked walk");
        }
        public void printMap() {
            int x = 0;
            int y = 0;

            /*int count = 0;
            for (char c = (char)0; c <= (char)1700; ++c) {
                ch.write("<Twhite>" + c + " <Tred>" + (0 + c) + " ");
                count++;
                if (count == 10) {
                    count = 0;
                    Console.Write("\n\r");
                }
            }
            Console.WriteLine();*/
            

            onBuilding = false;
            foreach (string[] row in myMap) {
                x = 0;
                foreach (string item in row) {
                    String mappoint;
                    switch (item) {
                        case "s":
                            mappoint = "<Twhite>S";
                            break;
                        case "r":
                            mappoint = "<Twhite>.";
                            break;
                        case "x":
                            mappoint = "<Tgray>#";
                            break;
                        case "b":
                            mappoint = "<Tyellow><Bblue>" + (char)127;
                            break;
                        case "t":
                            mappoint = "<Tdgreen>" + (char)6 + "";
                            break;
                        default:
                            mappoint = item;
                            break;
                    }
                    if (x == xPosition && y == yPosition) {
                        ch.write("<Tgreen>" + (char)1);
                        if (item == "b") {
                            onBuilding = true;
                        }
                    } else {
                        ch.write(mappoint);
                    }
                    ch.write(" ");
                    x++;
                }
                y++;
                Console.Write("\n\r");
            }
        }

        // N 
        //WOE
        // S 
        private void route() {
            routes = new bool[] { false, false, false, false };

            s.tellStory("on_" + xPosition + "," + yPosition);
            ch.writeLine();

            routes[0] = canAcces(xPosition, yPosition - 1);//North
            if (routes[0]) {
                s.tellStory("" + xPosition + "," + (yPosition - 1), "<Tgreen>" + (char)30 + "<Tred> 1: ");
            } else {
                s.tellStory("" + xPosition + "," + (yPosition - 1), (char)30 + " 1: ");
            }
            routes[1] = canAcces(xPosition + 1, yPosition);//east
            if (routes[1]) {
                s.tellStory("" + (xPosition + 1) + "," + yPosition, "<Tgreen>" + (char)16 + "<Tred> 2: ");
            } else {
                s.tellStory("" + (xPosition + 1) + "," + yPosition, (char)16 + " 2: ");
            }
            routes[2] = canAcces(xPosition, yPosition + 1);//south
            if (routes[2]) {
                s.tellStory("" + xPosition + "," + (yPosition + 1), "<Tgreen>" + (char)31 + "<Tred> 3: ");
            } else {
                s.tellStory("" + xPosition + "," + (yPosition + 1), (char)31 + " 3: ");
            }
            routes[3] = canAcces(xPosition - 1, yPosition);//west
            if (routes[3]) {
                s.tellStory("" + (xPosition - 1) + "," + yPosition, "<Tgreen>" + (char)17 + "<Tred> 4: ");
            } else {
                s.tellStory("" + (xPosition - 1) + "," + yPosition, (char)17 + " 4: ");
            }
            if (onBuilding) {
                ch.writeLine("<Tyellow><Bblue>" + (char)127 + "<Breset><Tred> 5: Go into building");
                s.tellStory("" + xPosition + "," + (yPosition - 1));
            }
        }

        private bool canAcces(int xPos, int yPos) {
            //location on the map?
            if (xPos >= 0 && xPos < width) {
                if (yPos >= 0 && yPos < height) {
                    switch (myMap[yPos][xPos]) {
                        case "s":
                            return true;
                        case "r":
                            return true;
                        case "x":
                            return false;
                        case "b":
                            return true;
                        case "o":
                            return false;
                    }
                }
            }
            //no
            return false;
        }
    }
}
